<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>零基础学-Java-01 | 无人深空</title><meta name="keywords" content="Java"><meta name="author" content="zcm"><meta name="copyright" content="zcm"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="基本数据类型及对应封装类byte——Byte（8bit） short——Short（16bit） int——Integer（32bit） long——Long（64bit） float——Float（32bit） double——Double（64bit） char——Character（16bit） boolean——Boolean（1bit） &#x2F;&#x2F; 看基本数据类型长度用对应封装类的 SIZE">
<meta property="og:type" content="article">
<meta property="og:title" content="零基础学-Java-01">
<meta property="og:url" content="https://gitee.com/zcmmmm/zcmmmm/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-01/index.html">
<meta property="og:site_name" content="无人深空">
<meta property="og:description" content="基本数据类型及对应封装类byte——Byte（8bit） short——Short（16bit） int——Integer（32bit） long——Long（64bit） float——Float（32bit） double——Double（64bit） char——Character（16bit） boolean——Boolean（1bit） &#x2F;&#x2F; 看基本数据类型长度用对应封装类的 SIZE">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/Cover.jpg">
<meta property="article:published_time" content="2022-09-23T00:53:24.000Z">
<meta property="article:modified_time" content="2022-09-23T02:39:10.860Z">
<meta property="article:author" content="zcm">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/Cover.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://gitee.com/zcmmmm/zcmmmm/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-01/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '零基础学-Java-01',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-09-23 10:39:10'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/categories.css?v1"><link rel="stylesheet" href="/css/navigation.css?v1"><link rel="stylesheet" href="/css/custom.css?v1"><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2022/09/22/36LN4kcjYOd9HT8.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">43</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">无人深空</a></span><div id="menus"></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="nav-right"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i></a></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">零基础学-Java-01</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-23T00:53:24.000Z" title="发表于 2022-09-23 08:53:24">2022-09-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-09-23T02:39:10.860Z" title="更新于 2022-09-23 10:39:10">2022-09-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/">Java</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">8.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>35分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="零基础学-Java-01"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h3 id="基本数据类型及对应封装类"><a href="#基本数据类型及对应封装类" class="headerlink" title="基本数据类型及对应封装类"></a>基本数据类型及对应封装类</h3><p>byte——Byte（8bit）</p>
<p>short——Short（16bit）</p>
<p>int——Integer（32bit）</p>
<p>long——Long（64bit）</p>
<p>float——Float（32bit）</p>
<p>double——Double（64bit）</p>
<p>char——Character（16bit）</p>
<p>boolean——Boolean（1bit）</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// 看基本数据类型长度用对应封装类的 SIZE 成员</span><br><span class="line">System.out.println(&quot;基本类型：byte 二进制位数：&quot; + Byte.SIZE);  </span><br></pre></td></tr></table></figure>

<h3 id="自动类型转换（小精度转大精度）"><a href="#自动类型转换（小精度转大精度）" class="headerlink" title="自动类型转换（小精度转大精度）"></a>自动类型转换（小精度转大精度）</h3><p>char(16) - int(32) - long(64) - float(32) - double(64)</p>
<p>byte(8) - short(16) - int(32) - long(64) - float(32) - double(64)</p>
<ol>
<li>有多种类型的数据混合运算时，系统自动首先将所有数据转化为容量最大的数据类型再进行计算</li>
<li>自动类型转换只能小转大，大转小会报错（只能强转）</li>
<li>(byte，short) 和 char 之间不会相互自动转换</li>
<li>byte、short、char 三者之间可以计算，计算前都转为 int</li>
<li>boolean 不参与自动类型转换</li>
<li>自动提升原则：表达式结果的类型自动提升为操作数中最大的类型</li>
</ol>
<h3 id="强制类型转化（大精度转小精度——有细节丢失）"><a href="#强制类型转化（大精度转小精度——有细节丢失）" class="headerlink" title="强制类型转化（大精度转小精度——有细节丢失）"></a>强制类型转化（大精度转小精度——有细节丢失）</h3><ol>
<li><p>强转符号只针对最近的操作数有效，往往需使用小括号优先升级</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">int x = (int)10 * 3.5    // 错误，强转只对10有效，整体为double，不能转为int</span><br><span class="line">int y = (int)(10 * 3.5)  // 正确</span><br></pre></td></tr></table></figure>
</li>
<li><p>char 类型可以保存 int 的常量值，但不能保存 int 的变量值，需要强转</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">char c1 = 10;			// 正确（-128~127）即可</span><br><span class="line">int num = 10;</span><br><span class="line">char c2 = num;			// 错误</span><br><span class="line">char c3 = (char)num;	// 正确</span><br></pre></td></tr></table></figure>
</li>
<li><p>byte 和 short 类型进行运算时，当作 int 类型处理</p>
</li>
</ol>
<h3 id="基本数据类型转-String"><a href="#基本数据类型转-String" class="headerlink" title="基本数据类型转 String"></a>基本数据类型转 String</h3><p>String &#x3D; 基本数据类型 + “”</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">int num = 10;</span><br><span class="line">String str = num + &quot;&quot;;</span><br></pre></td></tr></table></figure>

<h3 id="String-转基本数据类型"><a href="#String-转基本数据类型" class="headerlink" title="String 转基本数据类型"></a>String 转基本数据类型</h3><p>用基本数据类型封装类的<code>parseXXX</code>方法</p>
<p>如果格式不正确就会抛出异常，见下代码<code>第 3 行</code></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">String str = &quot;123&quot;;</span><br><span class="line">int num1 = Integer.parseInt(str);				// 正确 123</span><br><span class="line">int num2 = Integer.parseInt(&quot;hello&quot;);			// 错误，抛出异常</span><br><span class="line">double num3 = Double.parseDouble(str);			// 正确 123.0</span><br><span class="line"></span><br><span class="line">boolean bool2 = Boolean.parseBoolean(&quot;true&quot;);	// 正确 true</span><br><span class="line"></span><br><span class="line">// 特殊情况：String 转 char</span><br><span class="line">// 取第一个字符</span><br><span class="line">char c = str.charAt(0);							// &#x27;1&#x27;</span><br></pre></td></tr></table></figure>

<h3 id="逻辑运算（与、或、非、异或）"><a href="#逻辑运算（与、或、非、异或）" class="headerlink" title="逻辑运算（与、或、非、异或）"></a>逻辑运算（与、或、非、异或）</h3><ol>
<li><p><strong>短路与 vs 逻辑与</strong></p>
<p>相同点：两个条件都为 true 时结果为 true</p>
<p>短路与 <code>&amp;&amp;</code>：第一个条件为 false， 结果直接判为 false，不判断第二个条件（若第二个条件有自加操作等不会执行）</p>
<p>逻辑与 <code>&amp;</code>：无论第一个条件是否为 false，都要判断第二个条件（即位运算中的<code>按位与</code>）</p>
<p>开发中使用<code>短路与</code>效率高</p>
</li>
<li><p><strong>短路或 vs 逻辑或 同理</strong></p>
</li>
<li><p><strong>逻辑异或</strong>（即位运算中的<code>按位异或</code>）：a ^ b，当 a 和 b 不同时为 true，相同为 false</p>
</li>
</ol>
<h3 id="复合赋值运算会进行类型转换"><a href="#复合赋值运算会进行类型转换" class="headerlink" title="复合赋值运算会进行类型转换"></a>复合赋值运算会进行类型转换</h3><p>复合赋值运算符即 <code>+=</code> 、<code>-=</code>、<code>/=</code>、<code>*=</code>等</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">byte b = 3;</span><br><span class="line">b += 2;		// 正确，b = (byte)(b + 2)，否则 int 不能赋值为 byte</span><br><span class="line">b++;		// 正确，b = (byte)(b + 1)</span><br></pre></td></tr></table></figure>

<h3 id="三元运算"><a href="#三元运算" class="headerlink" title="三元运算"></a>三元运算</h3><p>条件表达式 ？ 表达式1 ： 表达式2</p>
<p>可改为<code>if else</code>语句</p>
<p>条件为真：运算结果为表达式1，表达式2不执行；否则相反</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">int a = 10;</span><br><span class="line">int b = 99;</span><br><span class="line">int result = a &lt; b ? ++a : b--;</span><br><span class="line"></span><br><span class="line">// result = 11  a = 11  b = 99</span><br></pre></td></tr></table></figure>

<p>细节：<code>表达式1</code>和<code>表达式2</code>必须都可转为<code>被赋值对象</code>对应数据类型，也可手动强转</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">int a = 10;</span><br><span class="line">int b = 99;</span><br><span class="line">int result = a &lt; b ? 1.1 : 3.4; // 错误，double不能转为int</span><br><span class="line">int result = a &lt; b ? 1 : 3.4; 	// 错误，即使最终结果为 result = 1，但是表达式有 double</span><br></pre></td></tr></table></figure>

<h3 id="运算符优先级"><a href="#运算符优先级" class="headerlink" title="运算符优先级"></a>运算符优先级</h3><p>不用记，实际应用中不确定则加括号</p>
<p><img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEI5PH.png" alt="HEI5PH"></p>
<h3 id="标识符命名"><a href="#标识符命名" class="headerlink" title="标识符命名"></a>标识符命名</h3><p>​	Java 中除包名、静态常量等特殊情况，大部分情况下用<code>驼峰命名法</code>即可</p>
<ol>
<li><p><strong>命名规则</strong>：必须遵守，否则编译不通过</p>
<ul>
<li>由 <code>26 个英文字母大小写</code>、<code>0-9</code>、<code>_</code> 或 <code>$</code> 组成</li>
<li>不能由数字开头</li>
<li>不可以使用<code>关键字</code>和<code>保留字</code>，但可以包含</li>
<li>Java 中严格区分大小写，长度无限制</li>
<li>不含空格</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">\\ Java 语言目前定义了 <span class="number">51</span> 个关键字，这些关键字不能作为变量名、类名和方法名来使用。以下对这些关键字进行了分类</span><br><span class="line">\\ 关键字中所有字母均为小写</span><br><span class="line">\\ <span class="number">1.</span>数据类型：</span><br><span class="line"><span class="type">boolean</span>、<span class="type">int</span>、<span class="type">long</span>、<span class="type">short</span>、<span class="type">byte</span>、<span class="type">float</span>、<span class="type">double</span>、<span class="type">char</span>、class、interface</span><br><span class="line">\\ <span class="number">2.</span>流程控制：</span><br><span class="line"><span class="keyword">if</span>、<span class="keyword">else</span>、<span class="keyword">do</span>、<span class="keyword">while</span>、<span class="keyword">for</span>、<span class="keyword">switch</span>、<span class="keyword">case</span>、<span class="keyword">default</span>、<span class="keyword">break</span>、<span class="keyword">continue</span>、<span class="keyword">return</span>、<span class="keyword">try</span>、<span class="keyword">catch</span>、<span class="keyword">finally</span></span><br><span class="line">\\ <span class="number">3.</span>修饰符：</span><br><span class="line"><span class="keyword">public</span>、<span class="keyword">protected</span>、<span class="keyword">private</span>、<span class="keyword">final</span>、<span class="keyword">void</span>、<span class="keyword">static</span>、strict、<span class="keyword">abstract</span>、<span class="keyword">transient</span>、<span class="keyword">synchronized</span>、<span class="keyword">volatile</span>、<span class="keyword">native</span></span><br><span class="line">\\ <span class="number">4.</span>动作：</span><br><span class="line"><span class="keyword">package</span>、<span class="keyword">import</span>、<span class="keyword">throw</span>、<span class="keyword">throws</span>、extends、implements、<span class="built_in">this</span>、<span class="built_in">super</span>、<span class="keyword">instanceof</span>、<span class="keyword">new</span></span><br><span class="line">\\ <span class="number">5.</span>保留字：</span><br><span class="line"><span class="literal">true</span>、<span class="literal">false</span>、<span class="literal">null</span>、goto、const</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>命名规范</strong>：体现专业性</p>
<ul>
<li>包名：多单词组成时，所有字母都小写。如<code>com.hsp.crm</code></li>
<li>类名、接口名：多单词组成时，所有单词首字母大写。如<code>TankShotGame</code></li>
<li>变量名、方法名：多单词组成时，第一个单词首字母小写，其余单词首字母大写（即驼峰命名法）。如<code>tankShotGame</code></li>
<li>常量名：所有字母大写；多单词组成时单词间用<code>_</code>连接。如<code>TAX_RATE</code></li>
</ul>
</li>
</ol>
<h3 id="读取键盘输入"><a href="#读取键盘输入" class="headerlink" title="读取键盘输入"></a>读取键盘输入</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 表示导入 java.util 包下的 Scanner 类</span></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Input</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 1.创建 Scanner 类对象</span></span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">scanner</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        <span class="comment">// 接收键盘输入，如无输入则等待</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">str</span> <span class="operator">=</span> scanner.next();</span><br><span class="line">        <span class="type">int</span> <span class="variable">inum</span> <span class="operator">=</span> scanner.nextInt();</span><br><span class="line">        <span class="type">double</span> <span class="variable">dnum</span> <span class="operator">=</span> scanner.nextDouble();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="位运算"><a href="#位运算" class="headerlink" title="位运算"></a>位运算</h3><p>Java 中只有<code>有符号数</code>，没有<code>无符号数</code></p>
<p>计算机均以<code>补码</code>形式进行运算，表示结果用<code>原码</code></p>
<p><code>&lt;&lt;</code>：算数左移——符号位不参与移动，低位补0</p>
<p><code>&gt;&gt;</code>：算数右移——符号位不参与移动，符号位补高位</p>
<p><code>&gt;&gt;&gt;</code>：逻辑右移（无符号右移）——符号位参与移动，高位补0</p>
<p>无<code>&lt;&lt;&lt;</code>符号</p>
<h3 id="模运算"><a href="#模运算" class="headerlink" title="模运算"></a>模运算</h3><p>a % b &#x3D;&#x3D; a - (a &#x2F; b * b)</p>
<ol>
<li>a % b，当 a 为小数时，有类型转换，等价于 a - (int)(a) &#x2F; b * b</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">10.5 % 3 = 10.5 - (int)(10.5) / 3 * 3 = 1.5</span><br><span class="line"></span><br><span class="line">-10 % 3 = -10 - (-10 / 3 * 3) = -1</span><br><span class="line"></span><br><span class="line">-10.5 % 3 = -10.5 - (int)(-10.5) / 3 * 3 = -1.5</span><br><span class="line"></span><br><span class="line">// 有负数时，先忽略符号，结果符号同`被取模数（即左边数）`</span><br><span class="line">2 % 5 = 2</span><br><span class="line">2 % (-5) = 2</span><br><span class="line">5 % (-2) = 1</span><br><span class="line">(-2) % 5 = -2</span><br><span class="line">(-5) % 2 = -1</span><br><span class="line">(-2) % (-5) = -2</span><br><span class="line">(-5) % (-2) = -1</span><br></pre></td></tr></table></figure>

<h3 id="有小数参与的运算，结果为近似值"><a href="#有小数参与的运算，结果为近似值" class="headerlink" title="有小数参与的运算，结果为近似值"></a>有小数参与的运算，结果为近似值</h3><p>因为某些小数不能用二进制完整表示</p>
<h3 id="switch-细节"><a href="#switch-细节" class="headerlink" title="switch 细节"></a>switch 细节</h3><ol>
<li><p>switch 中的数据类型只能为<code>byte</code>、<code>short</code>、<code>int</code>、<code>char</code>、<code>enum</code>、<code>String</code></p>
</li>
<li><p>switch 中的数据类型应与 case 后的常量一致，或可以自动转换成相互比较的类型</p>
</li>
<li><p>case 后的值必须为常量，不能为变量</p>
</li>
<li><p>default 为可选，如不满足上述所有 case 则执行 default</p>
</li>
<li><p>break 用于跳出 switch，如无 break，则一直执行到遇到 break 或结尾</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">num</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="keyword">switch</span>(num) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">10</span>:	<span class="comment">// 正确</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok1&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">10</span>		<span class="comment">// 错误，case 之间不可重复</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok2&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">10</span> + <span class="number">1</span>	<span class="comment">// 正确，虽为表达式，但仍为常量</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok3&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;a&#x27;</span>:	<span class="comment">// 正确，int 和 char 可以比较</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok4&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&quot;a&quot;</span>:	<span class="comment">// 错误，String 和 int 不可以比较</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok5&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">10.0</span>:	<span class="comment">// 错误，double 和 int 不可以比较</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok6&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:	<span class="comment">// 可有可无</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok7&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">switch</span>(<span class="number">10</span>) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">5</span>:		<span class="comment">// 不匹配</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok0&quot;</span>);</span><br><span class="line">   	<span class="keyword">case</span> <span class="number">10</span>:	<span class="comment">// 匹配进入</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok1&quot;</span>);</span><br><span class="line">    <span class="keyword">case</span> <span class="number">20</span>:	<span class="comment">// 上匹配后无 break，不用判断当前 case</span></span><br><span class="line">        System.out.println(<span class="string">&quot;ok2&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;	<span class="comment">// break 后退出</span></span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;ok7&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出为 </span></span><br><span class="line"><span class="comment">// ok1</span></span><br><span class="line"><span class="comment">// ok2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 穿透 现象演示</span></span><br><span class="line"><span class="keyword">switch</span>(num) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">   	<span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;1~3&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">   	<span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">    <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;4~6&quot;</span>);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        System.out.println(<span class="string">&quot;others&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="break、continue-细节"><a href="#break、continue-细节" class="headerlink" title="break、continue 细节"></a>break、continue 细节</h3><p><strong>continue 同</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 可用标签 label 指定退出到xx位置——开发中不建议使用标签</span></span><br><span class="line"><span class="comment">// 不用则默认退出最近循环体</span></span><br><span class="line">label1:</span><br><span class="line">&#123;</span><br><span class="line">    label2:</span><br><span class="line">	&#123;</span><br><span class="line">		label3:		</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 指定退出到 label2</span></span><br><span class="line">            <span class="keyword">break</span> label2;</span><br><span class="line">		&#125;    </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><ol>
<li><p>数组默认值（类的属性也有默认值，与数组相同）</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">byte——0  short——0  int——0  long——0</span><br><span class="line">float——0.0  double——0.0</span><br><span class="line">// \u表明 16bit，char类型默认为 16bit 的 0 字符</span><br><span class="line">char——\u0000  </span><br><span class="line">boolean——false</span><br><span class="line">String——null</span><br></pre></td></tr></table></figure>
</li>
<li><p>数组默认为引用传递（类也是引用传递）</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">int[] arr1 = &#123;1, 2, 3&#125;</span><br><span class="line">int[] arr2 = arr1;</span><br><span class="line">arr2[0] = 10;</span><br><span class="line"></span><br><span class="line">// arr1 = [10, 2, 3]</span><br><span class="line">// arr2 = [10, 2, 3]</span><br></pre></td></tr></table></figure>
</li>
<li><p>数组属于引用类型，数组型数据是对象（object）</p>
</li>
<li><p>数组声明方法</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">// 一维</span><br><span class="line">int[] array  或  int array[]</span><br><span class="line">// 初始化</span><br><span class="line">int[] array = new int[5];	// 初始化为0</span><br><span class="line">int[] array = &#123;0, 0, 0, 0&#125;;	// 手动初始化</span><br><span class="line">int[] array = new int[]&#123;0, 0, 0, 0&#125;;	// 正确</span><br><span class="line">int[] array = new int[4]&#123;0, 0, 0, 0&#125;;	// 错误，不用指明长度</span><br><span class="line"></span><br><span class="line">// 二维</span><br><span class="line">int[][] array  或  int[] array[]  或 int array[][]</span><br><span class="line">// 初始化</span><br><span class="line">int[][] array = new int[5][];			// 正确，5个一维数组，各个长度不定</span><br><span class="line">int[][] array = new int[5][2];			// 正确，5 * 2</span><br><span class="line">int[][] array = new int[][]&#123;&#123;0,0&#125;&#125;;		// 正确，1 * 2</span><br><span class="line">int[][] array = new int[1][]&#123;&#123;0,0&#125;&#125;;	// 错误</span><br><span class="line">int[][] array = new int[][2]&#123;&#123;0,0&#125;&#125;;	// 错误</span><br><span class="line">int[][] array = new int[1][2]&#123;&#123;0,0&#125;&#125;;	// 错误</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="String"><a href="#String" class="headerlink" title="String"></a>String</h3><ol>
<li><p>String 是引用类型，指向一个地址</p>
</li>
<li><p>String 类型是常量，不能”修改“</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">String str = &quot;zcm&quot;;		// str 是字符串 zcm 的引用</span><br><span class="line">str.charAt(0) = &#x27;t&#x27;;	// 错误，不能修改</span><br><span class="line">str = &quot;try&quot;;			// 正确，将 str 指向字符串 try</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="类和对象"><a href="#类和对象" class="headerlink" title="类和对象"></a>类和对象</h3><ol>
<li><p>类和对象的内存分配机制</p>
<ul>
<li><p>栈：存放基本数据类型（局部变量）</p>
</li>
<li><p>堆：存放对象、数组等</p>
</li>
<li><p>方法区：常量池（如字符串）、类加载信息</p>
<p><img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEo6YQ.png" alt="HEo6YQ"></p>
</li>
</ul>
</li>
<li><p>Java 创建对象流程<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1fh411y7R8?p=200">简单分析</a>、<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1fh411y7R8?p=245&spm_id_from=pageDriver">详细分析(含构造器)</a></p>
<ul>
<li>加载类信息（属性和方法，一个类只会在第一次使用时被加载一次）</li>
<li>在堆中分配空间并进行默认初始化（初始值同数组）</li>
<li>（详细分析独有步骤：默认初始化 —— 显式初始化 —— 构造器初始化）</li>
<li>把地址赋给对象引用（对象名）</li>
</ul>
</li>
<li><p>类中成员一般为<code>private</code>，提供<code>public</code>的 get、set 方法</p>
</li>
<li><p>方法形参不能指定默认值（python 可以）</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Test</span> &#123;</span><br><span class="line">    <span class="comment">// b = 10 会报错</span></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">func</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b = <span class="number">10</span>)</span> &#123;</span><br><span class="line">		System.out.println(a + <span class="string">&quot; &quot;</span> + b);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>方法传参</p>
<ul>
<li><p>基本数据类型：值拷贝——方法中形参的任何改变都不影响实参</p>
</li>
<li><p>引用数据类型（如数组、类对象）：引用拷贝——实参随着形参的改变而变化</p>
</li>
<li><p>String 是引用数据类型，但由于 String 是常量不可”修改“（见上<code>String</code>），所以表现为<code>值拷贝</code></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">		<span class="type">String</span> <span class="variable">str1</span> <span class="operator">=</span> <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">		System.out.println(str1);	<span class="comment">// abc</span></span><br><span class="line">		str1 = <span class="string">&quot;zcm&quot;</span>;</span><br><span class="line">		System.out.println(str1);	<span class="comment">// zcm</span></span><br><span class="line">		test(str1);</span><br><span class="line">		System.out.println(str1);	<span class="comment">// zcm</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">test</span><span class="params">(String str)</span> &#123;</span><br><span class="line">	    str = <span class="string">&quot;try&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>同数组，类也是引用传递</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Test test1 = new Test();</span><br><span class="line">		System.out.println(test1.num);	// 10</span><br><span class="line">		</span><br><span class="line">		Test test2 = test1;</span><br><span class="line">		test2.num = 20;</span><br><span class="line">		System.out.println(test1.num);	// 20</span><br><span class="line">		System.out.println(test2.num);	// 20</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Test &#123;</span><br><span class="line">    public int num = 10;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>引用传参细节</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Person p = new Person();</span><br><span class="line">		p.name = &quot;zcm&quot;;</span><br><span class="line">		p.age = 10;</span><br><span class="line">		// 输出：before test zcm 10</span><br><span class="line">		System.out.println(&quot;before test &quot; + p.name + &quot; &quot; + p.age);</span><br><span class="line">		</span><br><span class="line">		Test test = new Test();</span><br><span class="line">		test.test1(p);</span><br><span class="line">		// 输出：test1 try 20</span><br><span class="line">		System.out.println(&quot;test1 &quot; + p.name + &quot; &quot; + p.age);</span><br><span class="line">		</span><br><span class="line">		test.test2(p);</span><br><span class="line">		// 输出：test2 try 20</span><br><span class="line">		System.out.println(&quot;test2 &quot; + p.name + &quot; &quot; + p.age);</span><br><span class="line">		</span><br><span class="line">		test.test3(p);</span><br><span class="line">		// 输出：test3 try 20</span><br><span class="line">		System.out.println(&quot;test3 &quot; + p.name + &quot; &quot; + p.age);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Person &#123;</span><br><span class="line">    String name;</span><br><span class="line">    int age;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Test &#123;</span><br><span class="line">	// 引用传递，实参随之修改</span><br><span class="line">    public void test1(Person p) &#123;</span><br><span class="line">        p.name = &quot;try&quot;;</span><br><span class="line">        p.age = 20;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    // 形参 p 指向 null，不影响实参</span><br><span class="line">    public void test2(Person p) &#123;</span><br><span class="line">        p = null;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    // 形参 p 指向新的对象引用，不影响实参</span><br><span class="line">    public void test3(Person p) &#123;</span><br><span class="line">        p = new Person();</span><br><span class="line">        p.name = &quot;try&quot;;</span><br><span class="line">        p.age = 20;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>方法不能嵌套定义：方法里面不能再定义方法</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public class Test &#123;</span><br><span class="line">	public void func1() &#123;</span><br><span class="line">		// 错误，方法不能嵌套定义</span><br><span class="line">		public void func2() &#123;</span><br><span class="line">		</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>浅拷贝 vs 深拷贝</strong> </p>
<ul>
<li>浅拷贝：对基本数据类型进行值传递，对引用数据类型进行引用传递般的拷贝</li>
<li>深拷贝：对基本数据类型进行值传递，对引用数据类型，创建一个新的对象，并复制其内容</li>
<li>思考如何高效地深拷贝一个对象数组（不用循环）<ul>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/zc22/p/3484981.html">方法1</a></li>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/xjnotxj/p/9810534.html">方法2</a></li>
</ul>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		Person p = new Person();</span><br><span class="line">		p.name = &quot;zcm&quot;;</span><br><span class="line">		p.age = 10;</span><br><span class="line">		</span><br><span class="line">		Person sCopy = p.shallowCopy(p);</span><br><span class="line">		Person dCopy = p.deepCopy(p);</span><br><span class="line">		// p zcm 10</span><br><span class="line">		// sCopy zcm 10</span><br><span class="line">		// dCopy zcm 10</span><br><span class="line">		System.out.println(&quot;p &quot; + p.name + &quot; &quot; + p.age);</span><br><span class="line">		System.out.println(&quot;sCopy &quot; + sCopy.name + &quot; &quot; + sCopy.age);</span><br><span class="line">		System.out.println(&quot;dCopy &quot; + dCopy.name + &quot; &quot; + dCopy.age);</span><br><span class="line">		</span><br><span class="line">		p.name = &quot;try&quot;;</span><br><span class="line">		p.age = 20;</span><br><span class="line">		// p try 20</span><br><span class="line">		// sCopy try 20</span><br><span class="line">		// dCopy zcm 10</span><br><span class="line">		System.out.println(&quot;p &quot; + p.name + &quot; &quot; + p.age);</span><br><span class="line">		System.out.println(&quot;sCopy &quot; + sCopy.name + &quot; &quot; + sCopy.age);</span><br><span class="line">		System.out.println(&quot;dCopy &quot; + dCopy.name + &quot; &quot; + dCopy.age);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Person &#123;</span><br><span class="line">    String name;</span><br><span class="line">    int age;</span><br><span class="line">    </span><br><span class="line">    public Person shallowCopy(Person p) &#123;</span><br><span class="line">        Person copy = p;</span><br><span class="line">        return p;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    public Person deepCopy(Person p) &#123;</span><br><span class="line">        Person copy = new Person();</span><br><span class="line">        copy.name = p.name;</span><br><span class="line">        copy.age = p.age;</span><br><span class="line">        return copy;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="重写-即覆盖-Override-和重载-Overload-Java-重写-Override-与重载-Overload"><a href="#重写-即覆盖-Override-和重载-Overload-Java-重写-Override-与重载-Overload" class="headerlink" title="[重写(即覆盖 Override)和重载(Overload)](Java 重写(Override)与重载(Overload))"></a>[重写(即覆盖 Override)和重载(Overload)](Java 重写(Override)与重载(Overload))</h3><ul>
<li><p>重写（覆盖）：子类对父类的允许访问的方法的实现过程进行重新编写, 方法名、形参和返回值（返回值可不相同，见下）都不能改变。即外壳不变，核心重写</p>
<ul>
<li>返回值可不同，子类返回值可为父类返回值的子类</li>
<li>子类方法不能缩小父类方法的访问权限：public &gt; protected &gt; 默认 &gt; private</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span>&#123;</span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">move</span><span class="params">()</span>&#123;</span><br><span class="line">      System.out.println(<span class="string">&quot;动物可以移动&quot;</span>);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="keyword">public</span> Object <span class="title function_">cry</span><span class="params">()</span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">   &#125;</span><br><span class="line">    </span><br><span class="line">   <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">sleep</span><span class="params">()</span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">   &#125;</span><br><span class="line">    </span><br><span class="line">   <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">       </span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span>&#123;</span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">move</span><span class="params">()</span>&#123;</span><br><span class="line">      System.out.println(<span class="string">&quot;狗可以跑和走&quot;</span>);</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">// 返回值要么相同，要么子类的返回值是父类返回值的子类</span></span><br><span class="line">   <span class="comment">// 仍然构成重写，因为 String 是 Object 的子类</span></span><br><span class="line">   <span class="keyword">public</span> String <span class="title function_">cry</span><span class="params">()</span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span></span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">// 错误，若返回值不同且不为子类，但是方法名和参数列表相同，则报错</span></span><br><span class="line">   <span class="keyword">public</span> String <span class="title function_">sleep</span><span class="params">()</span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">   &#125;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">// 错误，缩小访问权限，父类 protected &gt; 子类 private</span></span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">       </span><br><span class="line">   &#125;</span><br><span class="line">    </span><br><span class="line">   <span class="comment">// 正确，可扩大访问权限，父类 protected &lt; 子类 public</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">eat</span><span class="params">()</span> &#123;</span><br><span class="line">       </span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>重载：在一个类里面，方法名字相同，形参列表不同（类型、个数或顺序不同，形参名称无所谓）。返回类型可以相同也可以不同。每个重载的方法（或者构造函数）都必须有一个独一无二的参数类型列表</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Overloading</span> &#123;</span><br><span class="line">	<span class="comment">// Overload 与返回值无关</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">test</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test1&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test</span><span class="params">(<span class="type">int</span> a)</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test2&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 报错，重复定义——形参名称无所谓</span></span><br><span class="line">    <span class="comment">// public void test(int b)&#123;</span></span><br><span class="line">    <span class="comment">//     System.out.println(&quot;test2&quot;);</span></span><br><span class="line">    <span class="comment">// &#125;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">//以下两个参数类型顺序不同</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">test</span><span class="params">(<span class="type">int</span> a,String s)</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test3&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;returntest3&quot;</span>;</span><br><span class="line">    &#125;   </span><br><span class="line"> </span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">test</span><span class="params">(String s,<span class="type">int</span> a)</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;test4&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;returntest4&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEoITU.png" alt="HEoITU"></p>
</li>
</ul>
<p>  重载（Overload）发生在本类；重写（即覆盖，Override）发生在父子类（不局限于直接父类）</p>
<ul>
<li><p>重载细节：优先参数匹配，参数不匹配时会考虑类型转换</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">    <span class="comment">// 方法重载，优先参数匹配，参数不匹配时会考虑类型转换</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        sum(<span class="number">10</span>, <span class="number">20</span>);		<span class="comment">// double + int 10.0 20</span></span><br><span class="line">        sum(<span class="number">10</span>, <span class="number">20.0</span>);		<span class="comment">// double + double 10.0 20.0</span></span><br><span class="line">        sum(<span class="number">10.0</span>, <span class="number">20</span>);		<span class="comment">// double + int 10.0 20</span></span><br><span class="line">        sum(<span class="number">10.0</span>, <span class="number">20.0</span>);	<span class="comment">// double + double 10.0 20.0</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">sum</span><span class="params">(<span class="type">double</span> num1, <span class="type">double</span> num2)</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;double + double &quot;</span> + num1 + <span class="string">&quot; &quot;</span> + num2);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">sum</span><span class="params">(<span class="type">double</span> num1, <span class="type">int</span> num2)</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;double + int &quot;</span> + num1 + <span class="string">&quot; &quot;</span> + num2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="可变参数"><a href="#可变参数" class="headerlink" title="可变参数"></a>可变参数</h3><ul>
<li><p>将同一个类中<strong>多个同名同功能</strong>但<strong>参数个数不同</strong>的方法封装成一个方法</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// int... 表接受类型为 int 的多个参数（0~n个）</span></span><br><span class="line"><span class="comment">// 当作数组使用</span></span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span>... nums)</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;参数个数 = &quot;</span> + nums.length);</span><br><span class="line">    <span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; nums.length; ++i) &#123;</span><br><span class="line">        sum += nums[i];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>可变参数的实参可为 0 个或任意多个</p>
</li>
<li><p>可变参数的本质是数组</p>
</li>
<li><p>可变参数的实参可为数组</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] array = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;;</span><br><span class="line">        <span class="comment">// 数组本身即为 “类型为 int 的多个参数（0~n个）”</span></span><br><span class="line">        System.out.println(sum(array));	<span class="comment">// 6</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// int... 表接受类型为 int 的多个参数（0~n个）</span></span><br><span class="line">    <span class="comment">// 当作数组使用</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span>... nums)</span> &#123;</span><br><span class="line">        <span class="comment">// 参数个数 = 3</span></span><br><span class="line">        System.out.println(<span class="string">&quot;参数个数 = &quot;</span> + nums.length);</span><br><span class="line">        <span class="type">int</span> <span class="variable">sum</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; nums.length; ++i) &#123;</span><br><span class="line">            sum += nums[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>可变参数可以与其他形参构成参数列表，但是可变参数必须放最后</p>
</li>
<li><p>一个形参列表中只能有一个可变参数</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public int sum(String name, int... nums) &#123;</span><br><span class="line">	// 正确，可变参数必须放最后</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public int sum(int... nums, String name) &#123;</span><br><span class="line">	// 错误，可变参数必须放最后</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public int sum(int... nums, String... name) &#123;</span><br><span class="line">	// 错误，一个形参列表中只能有一个可变参数</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="作用域（面向对象编程）"><a href="#作用域（面向对象编程）" class="headerlink" title="作用域（面向对象编程）"></a>作用域（面向对象编程）</h3><ul>
<li><p>局部变量：一般是只在成员方法中定义的变量（或在代码块中定义的变量），作用域为变量所在方法</p>
</li>
<li><p>全局变量：即属性，作用域为整个类体</p>
</li>
<li><p>全局变量有默认值，可直接使用；局部变量无默认值，必须先赋值后使用</p>
</li>
<li><p>全局变量和局部变量可以重名，访问时遵循就近原则</p>
</li>
<li><p>全局变量的生命周期较长 &#x3D;&#x3D; 对象的生命周期；局部变量生命周期 &#x3D;&#x3D; 方法 &#x2F; 代码块生命周期</p>
</li>
<li><p>全局变量可以被其他类通过<code>本类对象</code>使用；局部变量只能在本类对应方法中使用</p>
</li>
<li><p>全局变量可以加访问修饰符（public、protected、private等）；局部变量不可以加修饰符</p>
</li>
</ul>
<h3 id="构造器使用细节"><a href="#构造器使用细节" class="headerlink" title="构造器使用细节"></a>构造器使用细节</h3><ul>
<li><p>一个类可以有多个构造器，即构造器重载</p>
</li>
<li><p>构造器名 &#x3D;&#x3D; 类名</p>
</li>
<li><p><strong>构造器没有返回值</strong>，非构造器一定有返回值或返回 void</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    String name;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">(String pName)</span> &#123;</span><br><span class="line">        name = pName;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">(String pName, <span class="type">int</span> pAge)</span> &#123;</span><br><span class="line">        name = pName;</span><br><span class="line">        age = pAge;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>构造器完成对象的初始化，并不是创建对象</p>
</li>
<li><p>在创建对象时，系统自动调用该类的构造方法（不能手动调用<code>p.Person()</code>）</p>
</li>
<li><p>如果编程时没有定义构造器，系统会自动生成一个无参数的默认构造器</p>
</li>
<li><p>一旦自定义构造器，默认构造器就被覆盖，如需使用无参数构造器则需手动构造</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public class Main &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        // 正确，使用默认构造器 Person1()&#123;&#125;</span><br><span class="line">        Person1 p1 = new Person1();</span><br><span class="line">        </span><br><span class="line">        // 错误，默认构造器 Person2()&#123;&#125; 已经被覆盖</span><br><span class="line">        Person2 p2 = new Person2();</span><br><span class="line">        </span><br><span class="line">        // 正确，使用默认构造器 Person2(String pName, int pAge)&#123;&#125;</span><br><span class="line">        Person2 p2 = new Person2(&quot;zcm&quot;, 10);	</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Person1 &#123;</span><br><span class="line">    String name;</span><br><span class="line">    int age;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Person2 &#123;</span><br><span class="line">    String name;</span><br><span class="line">    int age;</span><br><span class="line">    </span><br><span class="line">    // 如需使用默认构造器 Person2()&#123;&#125;，则需显示定义如下</span><br><span class="line">    // public Person2()&#123;&#125;</span><br><span class="line">    </span><br><span class="line">    public Person2(String pName, int pAge) &#123;</span><br><span class="line">        name = pName;</span><br><span class="line">        age = pAge;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/u014418267/article/details/103356623">private 修饰的构造方法</a></p>
</li>
<li><p>借助 <code>this</code>，在一个构造器中访问另一个构造器</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// “this(参数列表)” 该语句只能在构造器中使用，用于访问另一构造器</span></span><br><span class="line"><span class="comment">// 该语句必须放在使用此语句的构造器的 第一条语句</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    String name;</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">()</span> &#123;</span><br><span class="line">    	<span class="comment">// 借助 this 访问另一个构造器</span></span><br><span class="line">        <span class="built_in">this</span>(<span class="string">&quot;zcm&quot;</span>, <span class="number">20</span>);</span><br><span class="line">        <span class="comment">// “this(参数列表)” 必须是第一条语句</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Person()&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Person</span><span class="params">(String name, <span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = pName;</span><br><span class="line">        <span class="built_in">this</span>.age = pAge;</span><br><span class="line">        System.out.println(<span class="string">&quot;Person(String name, int age)&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">func</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 错误，非构造器不能调用 “this(参数列表)” </span></span><br><span class="line">        <span class="built_in">this</span>(<span class="string">&quot;zcm&quot;</span>, <span class="number">20</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="包"><a href="#包" class="headerlink" title="包"></a>包</h3><ol>
<li><p>包的命名规则——只能包含数字、字母、下划线、小圆点，不能以数字开头，不能是关键字或保留字</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">demo.class.zcm	// 错误，class是关键字</span><br><span class="line">demo.1try.zcm	// 错误，不能以数字开头</span><br><span class="line">demo.try1.zcm	// 正确</span><br></pre></td></tr></table></figure>
</li>
<li><p>包的命名规范——com.公司名.项目名.业务模块名</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">com.sina.crm.user	// 用户模块</span><br><span class="line">com.sina.crm.order	// 订单模块</span><br><span class="line">com.sina.crm.utils	// 工具类</span><br></pre></td></tr></table></figure>
</li>
<li><p>常用的包</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">java.lang.*		<span class="comment">// lang包是基本包，默认引入，无需显示引入</span></span><br><span class="line">java.util.*		<span class="comment">// 工具包，如 Scanner</span></span><br><span class="line">java.net.*		<span class="comment">// 网络包，用于网络开发</span></span><br><span class="line">java.awt.*		<span class="comment">// GUI包，用于界面开发</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>包的导入——只导入需要用到的类，尽量不用<code>*</code></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;	<span class="comment">// 推荐，只导入 util 包下的 Scanner 类</span></span><br><span class="line"><span class="keyword">import</span> java.util.*;			<span class="comment">// 不推荐，导入 util 包下的所有类</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="访问修饰符（public、protected、default、private）"><a href="#访问修饰符（public、protected、default、private）" class="headerlink" title="访问修饰符（public、protected、default、private）"></a>访问修饰符（public、protected、default、private）</h3><ul>
<li>public：对外公开</li>
<li>protected：对子类和同一个包中的类公开</li>
<li>default（默认）：<strong>无修饰符</strong>，对同一个包的类公开</li>
<li>private：只有类本身可以访问，不对外公开</li>
</ul>
<p> <img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEoPs0.png" alt="HEoPs0"></p>
<ul>
<li>只有 public 和 default 可以修饰<code>类</code>，所有修饰符都可以修饰<code>方法</code>和<code>成员变量</code></li>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1fh411y7R8?p=280&spm_id_from=pageDriver">修饰符细节实例</a></li>
</ul>
<h3 id="面向对象-OOP-三大特征"><a href="#面向对象-OOP-三大特征" class="headerlink" title="面向对象(OOP)三大特征"></a><a target="_blank" rel="noopener" href="https://cloud.tencent.com/developer/article/1357504">面向对象(OOP)三大特征</a></h3><ul>
<li>封装（Encapsulation）：把客观事物封装成抽象的类，并且类可以把自己的数据和方法只让可信的类或者对象操作，对不可信的进行信息隐藏</li>
<li>继承（Inheritance）：使用现有类的所有功能，并在无需重新编写原来的类的情况下对这些功能进行扩展</li>
<li>多态（Polymorphism）：指一个类实例的相同方法在不同情形有不同表现形式。多态机制使具有不同内部结构的对象可以共享相同的外部接口</li>
</ul>
<h3 id="封装步骤"><a href="#封装步骤" class="headerlink" title="封装步骤"></a>封装步骤</h3><ol>
<li><p>将属性进行私有化（private）——不能直接修改属性</p>
</li>
<li><p>提供公共（public）的 set、get 方法——用于修改、获取该属性</p>
</li>
</ol>
<h3 id="继承细节（extends）"><a href="#继承细节（extends）" class="headerlink" title="继承细节（extends）"></a>继承细节（extends）</h3><ol>
<li><p>子类继承了所有属性和方法</p>
<ul>
<li><p>非私有的属性和方法：可以直接访问</p>
</li>
<li><p>私有（private）属性和方法：不能直接访问，要通过公共的 get、set 或其他调用方法</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 属性较为简单，直接 get、set 即可，在此只演示私有方法的调用</span></span><br><span class="line"><span class="comment">// 不能直接调用 func，写一个 public 的方法调用 func</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">func</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="comment">// doSomething</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">callFunc</span><span class="params">()</span> &#123;</span><br><span class="line">    func();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>子类必须调用父类的构造器，完成父类的初始化</p>
<ul>
<li><p>没有显式调用时，调用默认无参构造器</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// Base...</span></span><br><span class="line">        <span class="comment">// Sub...</span></span><br><span class="line">		<span class="type">Sub</span> <span class="variable">sub</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Sub</span>();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Base</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Base...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Sub</span> <span class="keyword">extends</span> <span class="title class_">Base</span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Sub</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 隐含 super(); 调用父类的无参构造器</span></span><br><span class="line">        <span class="comment">// super();	必须在第一行</span></span><br><span class="line">        System.out.println(<span class="string">&quot;Sub...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>若父类没有提供无参构造器，则必须在子类的构造器中用 <code>super(对应参数列表)</code>指定使用父类的某个构造器</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// Base(int num)...</span></span><br><span class="line">        <span class="comment">// Sub...</span></span><br><span class="line">		<span class="type">Sub</span> <span class="variable">sub</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Sub</span>();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> &#123;</span><br><span class="line">    <span class="type">int</span> num;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Base</span><span class="params">(<span class="type">int</span> num)</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Base(int num)...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Sub</span> <span class="keyword">extends</span> <span class="title class_">Base</span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Sub</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 父类无默认无参构造器</span></span><br><span class="line">        <span class="comment">// 指定 Base(int num) 构造器</span></span><br><span class="line">        <span class="comment">//  Base(int num) 必须在第一行</span></span><br><span class="line">        <span class="built_in">super</span>(<span class="number">10</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p><code>super()</code>必须在构造器的第一行（见上），且只能在构造器中使用</p>
</li>
<li><p><code>super()</code>和<code>this()</code>都只能放在构造器的第一行，因此这两个方法不能共存在一个构造器。（ this() 用于在一个构造器中访问另一个构造器）</p>
</li>
<li><p>Java 中所有类都是 <code>Object</code> 的子类</p>
</li>
<li><p>父类构造器的调用不限于直接父类，一直追溯到 <code>Object</code> 类</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">设继承关系为 Object -&gt; classA -&gt; classB -&gt; classC</span><br><span class="line">创建 classC 对象时</span><br><span class="line"><span class="comment">// Object 的构造器被调用</span></span><br><span class="line"><span class="comment">// classA 的构造器被调用</span></span><br><span class="line"><span class="comment">// classB 的构造器被调用</span></span><br><span class="line"><span class="comment">// classC 的构造器被调用</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>Java中只支持<strong>单继承机制</strong>，即子类最多只有一个直接父类；如需实现 <strong>A 同时继承 B 和 C</strong>，可通过 <strong>A 继承 B 继承 C</strong> 来间接实现</p>
</li>
<li><p>继承即子类和父类之间必须满足 <strong>子类 is a 父类</strong> 的关系，如 <strong><code>cat</code> is a <code>animal</code></strong></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1fh411y7R8?p=294">继承内存机制</a></p>
<p> <img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEoAdU.png" alt="HEoAdU"></p>
</li>
<li><p>继承后的属性&#x2F;方法访问</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">	    <span class="type">Son</span> <span class="variable">son</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Son</span>();</span><br><span class="line">	    <span class="comment">// 1.首先看子类有无该属性/方法</span></span><br><span class="line">	    <span class="comment">// 2.有且可以访问(看修饰符)则访问，否则3</span></span><br><span class="line">	    <span class="comment">// 3.看上一个父类有无该属性/方法，且是否可以访问</span></span><br><span class="line">	    <span class="comment">// 4.重复步骤3直到 Object 类，如都没有则报错</span></span><br><span class="line">		System.out.println(son.name);   <span class="comment">// 输出：son</span></span><br><span class="line">		System.out.println(son.hobby);  <span class="comment">// 输出：旅游</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 报错，Father 类中 age 为 private，不能在类外直接访问</span></span><br><span class="line">		<span class="comment">// 即使 GrandPa 类中有允许类外访问的 age，也会被 Father 阻塞</span></span><br><span class="line">		<span class="comment">// System.out.println(son.age);</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// 如需访问上级的 name</span></span><br><span class="line">		<span class="comment">// 则要求提供 getFatherName 等方法</span></span><br><span class="line">		System.out.println(son.getFatherName());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">GrandPa</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;GrandPa&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">hobby</span> <span class="operator">=</span> <span class="string">&quot;旅游&quot;</span>;</span><br><span class="line">    <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">60</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Father</span> <span class="keyword">extends</span> <span class="title class_">GrandPa</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;Father&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">30</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Son</span> <span class="keyword">extends</span> <span class="title class_">Father</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;Son&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getFatherName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">super</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>访问父类的属性和方法</p>
<ul>
<li><p>直接访问：子类中没有与父类同名的属性和方法且子类有权限访问（子类中没有与父类同名的属性和方法则 this 和 super 起到同样的访问父类属性和方法的效果）</p>
</li>
<li><p>通过 super 访问：子类中有与父类同名的属性和方法且子类有权限访问（super 会跳过本类，查找父类及以上，不限于直接父类）</p>
</li>
<li><p>通过 get、set 等方法访问：子类无访问权限</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Father</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;Father&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">hobby</span> <span class="operator">=</span> <span class="string">&quot;旅游&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">30</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.age;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Son</span> <span class="keyword">extends</span> <span class="title class_">Father</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;Son&quot;</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">accessFather</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// 1.直接访问：子类中没有与父类同名的属性和方法且子类有权限访问</span></span><br><span class="line">        System.out.println(hobby);			<span class="comment">// 旅游</span></span><br><span class="line">        System.out.println(<span class="built_in">this</span>.hobby);		<span class="comment">// 旅游</span></span><br><span class="line">        System.out.println(<span class="built_in">super</span>.hobby);	<span class="comment">// 旅游</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 2.通过 super 访问：子类中有与父类同名的属性和方法且子类有权限访问</span></span><br><span class="line">        System.out.println(<span class="built_in">super</span>.name);		<span class="comment">// Father</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 3.通过 get、set 等方法访问：子类无访问权限</span></span><br><span class="line">        System.out.println(<span class="built_in">super</span>.getAge());	<span class="comment">// 30</span></span><br><span class="line">        setAge(<span class="number">40</span>);		<span class="comment">// 子类中没有与父类同名的属性和方法且子类有权限访问</span></span><br><span class="line">        <span class="built_in">this</span>.setAge(<span class="number">40</span>);<span class="comment">// 子类中没有与父类同名的属性和方法且子类有权限访问</span></span><br><span class="line">        System.out.println(<span class="built_in">super</span>.getAge());	<span class="comment">// 40</span></span><br><span class="line">        <span class="built_in">super</span>.setAge(<span class="number">50</span>);</span><br><span class="line">        System.out.println(<span class="built_in">super</span>.getAge());	<span class="comment">// 50</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>super 和 this 的区别</p>
<p> <img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEopzn.png" alt="HEopzn"></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1fh411y7R8?p=348&spm_id_from=pageDriver">本章作业06</a></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 假定 Grand、Father、Son 在同一个包</span></span><br><span class="line"><span class="comment">// 父类和子类通过 this 和 super 分别可以调用哪些属性和方法</span></span><br><span class="line"><span class="comment">// 从上到下分析，先 Father 再 Son</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Grand</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;Grand&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">80</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">g1</span><span class="params">()</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Father</span> <span class="keyword">extends</span> <span class="title class_">Grand</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">id</span> <span class="operator">=</span> <span class="string">&quot;001&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">double</span> score;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">f1</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// super 可以访问</span></span><br><span class="line">        <span class="built_in">super</span>.name;  <span class="built_in">super</span>.g1();</span><br><span class="line">        <span class="comment">// this 可以访问</span></span><br><span class="line">        <span class="built_in">this</span>.id;  <span class="built_in">this</span>.score;  <span class="built_in">this</span>.f1();  <span class="built_in">this</span>.name;  <span class="built_in">this</span>.g1();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Son</span> <span class="keyword">extends</span> <span class="title class_">Father</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;Son&quot;</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">g1</span><span class="params">()</span> &#123;&#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">show</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">// super 可以访问</span></span><br><span class="line">        <span class="built_in">super</span>.name;  <span class="built_in">super</span>.g1();  <span class="built_in">super</span>.id;  <span class="built_in">super</span>.f1();</span><br><span class="line">        <span class="comment">// this 可以访问</span></span><br><span class="line">        <span class="built_in">this</span>.id;  <span class="built_in">this</span>.f1();  <span class="built_in">this</span>.name;  <span class="built_in">this</span>.g1();  <span class="built_in">this</span>.show();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>细节</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// ArrayList&lt;ArryList&lt;Integer&gt;&gt; can not convert to List&lt;List&lt;Integer&gt;&gt;</span></span><br><span class="line"><span class="comment">// 以下语句会引起上述错误</span></span><br><span class="line">ArrayList&lt;ArrayList&lt;Integer&gt;&gt; test1 = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">List&lt;List&lt;Integer&gt;&gt; test2 = test1;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 解释：</span></span><br><span class="line"><span class="comment">// ArrayList 是 List 的子类，ArrayList 可以转为 List</span></span><br><span class="line"><span class="comment">// ArrayList&lt;基本封装类&gt; 可以转为 List&lt;相同基本封装类&gt;</span></span><br><span class="line"><span class="comment">// ArrayList&lt;非基本封装类&gt; 不可以转为 List&lt;(不)相同非基本封装类&gt;；因为两者间没有继承关系</span></span><br><span class="line"></span><br><span class="line">--------------------------------------------------------------------</span><br><span class="line"><span class="comment">// 类似有如下规则</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Father</span>&#123;&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Son</span> <span class="keyword">extends</span> <span class="title class_">Father</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Son 是 Father 的子类</span></span><br><span class="line"><span class="comment">// Son[] 是 Father[] 的子类</span></span><br><span class="line"><span class="comment">// ArrayList&lt;Son&gt; 不是 ArrayList&lt;Father&gt; 的子类</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="多态（polymorphic）"><a href="#多态（polymorphic）" class="headerlink" title="多态（polymorphic）"></a>多态（polymorphic）</h3><ul>
<li><p>含义：方法或对象具有多种形态。多态是OOP的第三大特征，是建立在封装和继承的基础之上的</p>
</li>
<li><p>方法多态：<strong>重写</strong>和<strong>重载</strong>即体现多态</p>
</li>
<li><p>对象多态：</p>
<ul>
<li><p>一个对象的编译类型和运行类型可以不一致</p>
</li>
<li><p>编译类型在定义对象时确定，不可改变</p>
</li>
<li><p>运行类型是可以变化的</p>
</li>
<li><p>编译类型看定义时<code>=</code>的左边；运行类型看<code>=</code>的右边</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animal</span> <span class="variable">animal</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();<span class="comment">// animal 的编译类型是 Animal，运行类型是 Dog</span></span><br><span class="line">animal = <span class="keyword">new</span> <span class="title class_">Cat</span>();		  <span class="comment">// animal 的编译类型仍是 Animal，运行类型变为 Cat</span></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>注意事项和细节（以下父类都不局限于直接父类）</p>
<ul>
<li><p>向上转型：父类的引用指向了子类的对象，如<code>Animal animal = new Dog();</code></p>
<ul>
<li><p>此时可以调用父类中的所有成员（需遵守访问权限）</p>
</li>
<li><p>不能调用子类中的特有成员（因为能否调用是由编译类型决定的）</p>
</li>
<li><p>最终运行效果看子类的具体实现（运行时由运行类型决定——同继承调用，父子都有则用子，一级一级往上找）</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animal</span> <span class="variable">animal</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();</span><br><span class="line">Animal.cry();	<span class="comment">// 正确，可以调用父类中的所有成员</span></span><br><span class="line">Animal.run();	<span class="comment">// 错误，不能调用子类中的特有成员</span></span><br><span class="line">Animal.sleep();	<span class="comment">// 正确，输出 Dog sleep，最终运行效果看子类的具体实现</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">sleep</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Animal sleep&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">cry</span><span class="params">()</span>&#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">sleep</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Dog sleep&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Dog run&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>向下转型：父类引用强转为子类引用</p>
<ul>
<li><p>只能强转父类引用，不能强转父类对象</p>
</li>
<li><p>要求父类的引用必须指向当前目标类型的对象</p>
</li>
<li><p>向下转型后，可以调用子类类型中的所有成员</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animal</span> <span class="variable">animal</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();	<span class="comment">// 向上转型，animal 指向 Dog 对象</span></span><br><span class="line"><span class="type">Dog</span> <span class="variable">dog</span> <span class="operator">=</span> (Dog)animal;		<span class="comment">// 向下转型，此时可以调用子类 Dog 特有成员和方法</span></span><br><span class="line"><span class="type">Cat</span> <span class="variable">cat</span> <span class="operator">=</span> (Cat)animal;		<span class="comment">// 错误，父类的引用必须指向当前目标类型（为 Dog）的对象</span></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>属性没有重写之说，属性值看编译类型</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Animal</span> <span class="variable">animal</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();	</span><br><span class="line">System.out.println(animal.age);		<span class="comment">// 10，animal 编译类型为 animal</span></span><br><span class="line"><span class="type">Dog</span> <span class="variable">dog</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();</span><br><span class="line">System.out.println(dog.age);		<span class="comment">// 20，dog 编译类型为 dog</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">	<span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">age</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>（子类） <code>instanceof</code> （父类 &#x2F; 子类本身） &#x3D;&#x3D; true（看的是运行时类型）</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Dog</span> <span class="variable">dog</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();						<span class="comment">// dog 的运行时类型是 Dog</span></span><br><span class="line">System.out.println(dog <span class="keyword">instanceof</span> Dog);		<span class="comment">// true</span></span><br><span class="line">System.out.println(dog <span class="keyword">instanceof</span> Animal);	<span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="type">Animal</span> <span class="variable">animal</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Dog</span>();						<span class="comment">// animal 的运行时类型是 Dog</span></span><br><span class="line">System.out.println(animal <span class="keyword">instanceof</span> Dog);		<span class="comment">// true</span></span><br><span class="line">System.out.println(animal <span class="keyword">instanceof</span> Animal);	<span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span> &#123;&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span> <span class="keyword">extends</span> <span class="title class_">Animal</span> &#123;&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>课堂练习</p>
<p> <img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEIqqf.png" alt="HEIqqf"></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Sub</span> <span class="variable">s</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Sub</span>();	<span class="comment">// s 的编译类型和运行类型均为 Sub</span></span><br><span class="line">System.out.println(s.count);	<span class="comment">// 20</span></span><br><span class="line">s.display();					<span class="comment">// 20</span></span><br><span class="line"></span><br><span class="line"><span class="type">Base</span> <span class="variable">b</span> <span class="operator">=</span> s;	<span class="comment">// 向上转型，b 的编译类型为 Base，运行类型为 Sub</span></span><br><span class="line">System.out.println(b == s);		<span class="comment">// true</span></span><br><span class="line">System.out.println(b.count);	<span class="comment">// 10；属性无重写</span></span><br><span class="line">b.display();					<span class="comment">// 20；方法有重写</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">display</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="built_in">this</span>.count);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Sub</span> <span class="keyword">extends</span> <span class="title class_">Base</span> &#123;</span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">display</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="built_in">this</span>.count);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p><strong>动态绑定机制</strong></p>
<p>调用<code>对象方法</code>时，该方法会和该对象的<code>内存地址 / 运行类型</code>绑定</p>
<p>调用<code>对象属性</code>时，没有动态绑定机制，哪里声明哪里使用</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">A</span> <span class="variable">a</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">B</span>();	<span class="comment">// 向上转型，a 的编译类型为 A，运行类型为 B</span></span><br><span class="line"><span class="comment">// 输出：40（B 的 i + 20）</span></span><br><span class="line"><span class="comment">// 若子类 B 中的 sum() 方法被删除，则调用父类 A 中的 sum() 方法</span></span><br><span class="line"><span class="comment">// 但是 A 中 sum() 方法调用的 getI() 方法为调用运行类型的 getI()，即 B 的 getI()，返回 B 中的 i 为 20</span></span><br><span class="line"><span class="comment">// 所以若子类 B 中的 sum() 方法被删除，则输出 30</span></span><br><span class="line">System.out.println(a.sum());</span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出：30（B 的 i + 10）</span></span><br><span class="line"><span class="comment">// 若子类 B 中的 sum1() 方法被删除，则调用父类 A 中的 sum1() 方法</span></span><br><span class="line"><span class="comment">// A 中的 sum1() 方法使用的 i 即为 A 类的 i</span></span><br><span class="line"><span class="comment">// 所以若子类 B 中的 sum1() 方法被删除，则输出 20</span></span><br><span class="line">System.out.println(a.sum1());</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span> &#123;	<span class="comment">// 父类</span></span><br><span class="line">	<span class="keyword">public</span> <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getI</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">sum</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> getI() + <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">sum1</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> i + <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> B <span class="keyword">extends</span> <span class="title class_">A</span> &#123;	<span class="comment">// 子类</span></span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">20</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getI</span><span class="params">()</span> &#123; </span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">sum</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> i + <span class="number">20</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">sum1</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> i + <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>多态数组</strong>——数组的定义类型为父类类型，数组中的元素为子类类型</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">Person</span> <span class="variable">persons</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Persons</span>[<span class="number">5</span>];</span><br><span class="line">persons[<span class="number">0</span>] = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&quot;jack0&quot;</span>);</span><br><span class="line">persons[<span class="number">1</span>] = <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;jack1&quot;</span>, <span class="number">100.0</span>);</span><br><span class="line">persons[<span class="number">2</span>] = <span class="keyword">new</span> <span class="title class_">Student</span>(<span class="string">&quot;jack2&quot;</span>, <span class="number">30.0</span>);</span><br><span class="line">persons[<span class="number">3</span>] = <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;jack3&quot;</span>, <span class="number">5000.0</span>);</span><br><span class="line">persons[<span class="number">4</span>] = <span class="keyword">new</span> <span class="title class_">Teacher</span>(<span class="string">&quot;jack4&quot;</span>, <span class="number">10000.0</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &gt; persons.length; ++i) &#123;</span><br><span class="line">    System.out.println(persons[i].info());</span><br><span class="line">    <span class="comment">// 调用特有方法时先判断类</span></span><br><span class="line">    <span class="keyword">if</span>(persons[i] <span class="keyword">instanceof</span> Student) &#123;</span><br><span class="line">        <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> (Student)persons[i];	<span class="comment">// 向下转型</span></span><br><span class="line">        student.study();						<span class="comment">// 调用私有方法</span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span>(persons[i] <span class="keyword">instanceof</span> Teacher) &#123;</span><br><span class="line">        (Teacher)persons[i].teach();			<span class="comment">// 向下转型与调用</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> String name;</span><br><span class="line">	<span class="keyword">public</span> <span class="title function_">Person</span><span class="params">(String name)</span> &#123;</span><br><span class="line">		<span class="built_in">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> String <span class="title function_">info</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;name=&quot;</span> + <span class="built_in">this</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span> <span class="keyword">extends</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">double</span> score;</span><br><span class="line">	<span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">double</span> score)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(name);</span><br><span class="line">        <span class="built_in">this</span>.score = score;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> String <span class="title function_">info</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">super</span>.info() + <span class="string">&quot; score=&quot;</span> + <span class="built_in">this</span>.score;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 特有方法</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">study</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;学生 &quot;</span> + <span class="built_in">this</span>.name + <span class="string">&quot; 正在学习...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Teacher</span> <span class="keyword">extends</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">double</span> salary;</span><br><span class="line">	<span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(String name, <span class="type">double</span> salary)</span> &#123;</span><br><span class="line">        <span class="built_in">super</span>(name);</span><br><span class="line">        <span class="built_in">this</span>.salary = salary;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> String <span class="title function_">info</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">super</span>.info() + <span class="string">&quot; salary=&quot;</span> + <span class="built_in">this</span>.salary;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 特有方法</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">teach</span><span class="params">()</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;老师 &quot;</span> + <span class="built_in">this</span>.name + <span class="string">&quot; 正在教书...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>多态参数——方法形参为父类类型，实参为子类类型</p>
</li>
</ul>
<h3 id="Object-所有成员属性和方法"><a href="#Object-所有成员属性和方法" class="headerlink" title="Object 所有成员属性和方法"></a>Object 所有成员属性和方法</h3><p>Object 是最基本的类，是所有类的父类</p>
<p> <img src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/HEIOZ8.png" alt="HEIOZ8"></p>
<h3 id="x3D-x3D-和-equals-的对比"><a href="#x3D-x3D-和-equals-的对比" class="headerlink" title="&#x3D;&#x3D; 和 equals 的对比"></a>&#x3D;&#x3D; 和 equals 的对比</h3><ul>
<li>&#x3D;&#x3D; 是比较运算符；equals 是 Object 类方法，Java类都可以使用 equals</li>
<li>&#x3D;&#x3D; 判断基本类型值相等，判断引用类型地址相等（即是否为同一个对象）</li>
<li>equals <strong>只能判断引用类型</strong>，默认判断地址是否相等（即 Object 类中 equals 判断是否为同一个对象）；但是子类（如 String、Integer 类）一般需重写，判断内容是否相等</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="type">int</span> <span class="variable">it</span> <span class="operator">=</span> <span class="number">65</span>;</span><br><span class="line"><span class="type">float</span> <span class="variable">fl</span> <span class="operator">=</span> <span class="number">65.0f</span>;</span><br><span class="line">System.out.println(it == fl);	<span class="comment">// true，基本数据类型判断值相等</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// char 的本质为 int</span></span><br><span class="line"><span class="type">char</span> <span class="variable">ch1</span> <span class="operator">=</span> <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line"><span class="type">char</span> <span class="variable">ch2</span> <span class="operator">=</span> <span class="number">12</span>;</span><br><span class="line">System.out.println(ch1 == <span class="number">65</span>);	<span class="comment">// true，基本数据类型判断值相等</span></span><br><span class="line">System.out.println(ch2 == <span class="number">12</span>);	<span class="comment">// true，基本数据类型判断值相等</span></span><br><span class="line"></span><br><span class="line">System.out.println(<span class="string">&quot;hello&quot;</span> == <span class="number">65</span>);	<span class="comment">// 编译错误，String 和 int类型无法比较</span></span><br></pre></td></tr></table></figure>

<h3 id="hashCode-方法"><a href="#hashCode-方法" class="headerlink" title="hashCode 方法"></a>hashCode 方法</h3><ol>
<li>提高具有哈希结构的容器的效率</li>
<li>两个引用如果指向同一个对象，则哈希值相等</li>
<li>两个引用如果指向不同对象，则哈希值不等</li>
<li>哈希值是根据地址计算，但不能将哈希值等价为地址</li>
<li>hashCode 方法在某些集合中被重写</li>
</ol>
<h3 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString"></a>toString</h3><ul>
<li><p>默认返回：全类名（包名+类名） + @ + 哈希值的十六进制</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">全类名：getClass().getName()</span><br><span class="line">哈希值的十六进制：toHexString(hashCode())</span><br></pre></td></tr></table></figure>
</li>
<li><p>子类往往重写，一般用于输出对象属性</p>
</li>
<li><p>直接输出一个对象时，toString 方法会被默认调用</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// person 为 Person 的对象实例，以下两个语句输出相同</span></span><br><span class="line">System.out.println(person);</span><br><span class="line">System.out.println(person.toString());</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="finalize"><a href="#finalize" class="headerlink" title="finalize"></a>finalize</h3><ol>
<li>当对象被回收时，系统自动调用该对象的 finalize 方法；子类可以重写该方法，做一些释放资源的操作</li>
<li>JVM 在空闲时执行垃圾回收，有系统决定什么时候调用垃圾回收算法。可以用<code>System.gc()</code>主动调用垃圾回收器（该调用可能不被响应）</li>
<li>在实际开发中几乎不会运用 <code>finalize</code>方法</li>
</ol>
<h3 id="Java垃圾回收机制"><a href="#Java垃圾回收机制" class="headerlink" title="Java垃圾回收机制"></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/xiaoxi/p/6486852.html">Java垃圾回收机制</a></h3><h3 id="断点调试"><a href="#断点调试" class="headerlink" title="断点调试"></a>断点调试</h3></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/zcmmmm/zcmmmm">zcm</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/zcmmmm/zcmmmm/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-01/">https://gitee.com/zcmmmm/zcmmmm/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-01/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/zcmmmm/zcmmmm" target="_blank">无人深空</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a></div><div class="post_share"><div class="social-share" data-image="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/Cover.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-02/"><img class="prev-cover" src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/Cover.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">零基础学-Java-02</div></div></a></div><div class="next-post pull-right"><a href="/2020/08/17/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%A6%82%E4%BD%95%E8%AF%BB%E5%8F%96%E6%9C%AC%E5%9C%B0txt%E3%80%81json%E7%AD%89%E6%96%87%E4%BB%B6%E7%9A%84%E6%95%B0%E6%8D%AE(%E9%81%BF%E5%85%8D%E4%B9%B1%E7%A0%81)/"><img class="next-cover" src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/Applet-Wechat/Applet%20of%20WeChat.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">微信小程序如何读取本地txt、json等文件的数据(避免乱码)</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/03/03/Java%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="Java函数式编程"><img class="cover" src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/FunctionalProgramming/Functional%20programmi.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-03</div><div class="title">Java函数式编程</div></div></a></div><div><a href="/2023/04/17/Java%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9%E6%96%87%E7%AB%A0%E6%B1%87%E6%80%BB/" title="Java 基础知识点文章汇总"><img class="cover" src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-17</div><div class="title">Java 基础知识点文章汇总</div></div></a></div><div><a href="/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-02/" title="零基础学-Java-02"><img class="cover" src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/Cover.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-23</div><div class="title">零基础学-Java-02</div></div></a></div><div><a href="/2022/09/23/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6-Java-03/" title="零基础学-Java-03"><img class="cover" src="https://study-record-images.oss-cn-beijing.aliyuncs.com/Java/%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%AD%A6Java/Cover.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-23</div><div class="title">零基础学-Java-03</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%AF%B9%E5%BA%94%E5%B0%81%E8%A3%85%E7%B1%BB"><span class="toc-text">基本数据类型及对应封装类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%8A%A8%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%88%E5%B0%8F%E7%B2%BE%E5%BA%A6%E8%BD%AC%E5%A4%A7%E7%B2%BE%E5%BA%A6%EF%BC%89"><span class="toc-text">自动类型转换（小精度转大精度）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E5%8C%96%EF%BC%88%E5%A4%A7%E7%B2%BE%E5%BA%A6%E8%BD%AC%E5%B0%8F%E7%B2%BE%E5%BA%A6%E2%80%94%E2%80%94%E6%9C%89%E7%BB%86%E8%8A%82%E4%B8%A2%E5%A4%B1%EF%BC%89"><span class="toc-text">强制类型转化（大精度转小精度——有细节丢失）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%BD%AC-String"><span class="toc-text">基本数据类型转 String</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#String-%E8%BD%AC%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">String 转基本数据类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%EF%BC%88%E4%B8%8E%E3%80%81%E6%88%96%E3%80%81%E9%9D%9E%E3%80%81%E5%BC%82%E6%88%96%EF%BC%89"><span class="toc-text">逻辑运算（与、或、非、异或）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%8D%E5%90%88%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E4%BC%9A%E8%BF%9B%E8%A1%8C%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">复合赋值运算会进行类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97"><span class="toc-text">三元运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">运算符优先级</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E8%AF%86%E7%AC%A6%E5%91%BD%E5%90%8D"><span class="toc-text">标识符命名</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E9%94%AE%E7%9B%98%E8%BE%93%E5%85%A5"><span class="toc-text">读取键盘输入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%8D%E8%BF%90%E7%AE%97"><span class="toc-text">位运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A8%A1%E8%BF%90%E7%AE%97"><span class="toc-text">模运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9C%89%E5%B0%8F%E6%95%B0%E5%8F%82%E4%B8%8E%E7%9A%84%E8%BF%90%E7%AE%97%EF%BC%8C%E7%BB%93%E6%9E%9C%E4%B8%BA%E8%BF%91%E4%BC%BC%E5%80%BC"><span class="toc-text">有小数参与的运算，结果为近似值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#switch-%E7%BB%86%E8%8A%82"><span class="toc-text">switch 细节</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#break%E3%80%81continue-%E7%BB%86%E8%8A%82"><span class="toc-text">break、continue 细节</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84"><span class="toc-text">数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#String"><span class="toc-text">String</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1"><span class="toc-text">类和对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E5%86%99-%E5%8D%B3%E8%A6%86%E7%9B%96-Override-%E5%92%8C%E9%87%8D%E8%BD%BD-Overload-Java-%E9%87%8D%E5%86%99-Override-%E4%B8%8E%E9%87%8D%E8%BD%BD-Overload"><span class="toc-text">[重写(即覆盖 Override)和重载(Overload)](Java 重写(Override)与重载(Overload))</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0"><span class="toc-text">可变参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F%EF%BC%88%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B%EF%BC%89"><span class="toc-text">作用域（面向对象编程）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%99%A8%E4%BD%BF%E7%94%A8%E7%BB%86%E8%8A%82"><span class="toc-text">构造器使用细节</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8C%85"><span class="toc-text">包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E4%BF%AE%E9%A5%B0%E7%AC%A6%EF%BC%88public%E3%80%81protected%E3%80%81default%E3%80%81private%EF%BC%89"><span class="toc-text">访问修饰符（public、protected、default、private）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1-OOP-%E4%B8%89%E5%A4%A7%E7%89%B9%E5%BE%81"><span class="toc-text">面向对象(OOP)三大特征</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%85%E6%AD%A5%E9%AA%A4"><span class="toc-text">封装步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%A7%E6%89%BF%E7%BB%86%E8%8A%82%EF%BC%88extends%EF%BC%89"><span class="toc-text">继承细节（extends）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E6%80%81%EF%BC%88polymorphic%EF%BC%89"><span class="toc-text">多态（polymorphic）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Object-%E6%89%80%E6%9C%89%E6%88%90%E5%91%98%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95"><span class="toc-text">Object 所有成员属性和方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#x3D-x3D-%E5%92%8C-equals-%E7%9A%84%E5%AF%B9%E6%AF%94"><span class="toc-text">&#x3D;&#x3D; 和 equals 的对比</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hashCode-%E6%96%B9%E6%B3%95"><span class="toc-text">hashCode 方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#toString-NaN"><span class="toc-text">toString</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#finalize"><span class="toc-text">finalize</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Java%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6"><span class="toc-text">Java垃圾回收机制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%AD%E7%82%B9%E8%B0%83%E8%AF%95"><span class="toc-text">断点调试</span></a></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By zcm</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script src="/js/categories.js?v1"></script><script src="/js/navigation.js?v1"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>